package com.woniuxy.cloud.carl.carlgoods.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cloud.Aspect.log.Log;
import com.woniuxy.cloud.carl.carlgoods.client.SpuQueryClient;
import com.woniuxy.cloud.carl.carlgoods.client.dto.SpuDto;
import com.woniuxy.cloud.carl.carlgoods.client.param.FindSpuByContionParam;
import com.woniuxy.cloud.carl.carlgoods.client.util.MyPage;
import com.woniuxy.cloud.carl.carlgoods.infra.mapper.SpuMapper;
import com.woniuxy.cloud.carl.carlgoods.infra.model.Spu;
import com.woniuxy.result.Result;
import com.woniuxy.utils.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@RestController
@Slf4j
@CrossOrigin("*")
public class SpuQueryImpl implements SpuQueryClient {
    @Resource
    private SpuMapper spuMapper;


    @Override
    @Log("根据条件搜索spu")
    public Result<MyPage<SpuDto>> findSpuByCondition(FindSpuByContionParam param) {
        String condition = param.getCondition();
        Integer pageNum = param.getPageNum();
        Integer pageSize = param.getPageSize();
        if(pageNum==null||pageSize==null){
            pageNum=1;
            pageSize=3;
        }
        Page<Spu> page = new Page<>(pageNum, pageSize);
        MyPage<SpuDto> myPage= new MyPage<>();
        if(condition==null||condition.equals("")){

    spuMapper.selectPage(page,null);
}else {

    QueryWrapper<Spu> queryWrapper = new QueryWrapper<>();
    queryWrapper.like("spu_name",condition)
            .or().like("category_name",condition)
            .or().like("brand",condition).or().eq("spu_id",condition);
    spuMapper.selectPage(page,queryWrapper);
}
        System.out.println(page.toString());
        List<SpuDto> spuDtos = new ArrayList<>();

        for (Spu spu : page.getRecords()) {

            SpuDto spuDto = BeanUtil.toBean(spu, SpuDto.class);
            if(spuDto.getSpuStatus()==1){
                spuDto.setSpuStatusName("上架");
            }else {
                spuDto.setSpuStatusName("下架");
            }
            spuDtos.add(spuDto);
        }
        myPage.setPages(page.getPages());
        myPage.setPageNum(page.getCurrent());
        myPage.setTatol(page.getTotal());
        myPage.setPageSize(page.getSize());
        myPage.setList(spuDtos);

        return Result.success(myPage);
    }

    @Override
    public Result<SpuDto> findSpuById(Long spuId) {
        Spu spu = spuMapper.selectById(spuId);
        Assert.state(spu!=null,"该种商品类型不存在");
        SpuDto spuDto = BeanUtil.toBean(spu, SpuDto.class);
        return Result.success(spuDto);
    }

    @Override
    public Result<List<SpuDto>> findSpuBycategoryId(Long categoryId) {
        QueryWrapper<Spu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryId);
        List<Spu> spus = spuMapper.selectList(queryWrapper);
        List<SpuDto> spuDtos = BeanUtil.copyToList(spus, SpuDto.class);
        return Result.success(spuDtos);
    }
}
